package com.qianclass.talklive.app.view.Graffiti;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;

/**
 * 涂鸦底色
 */
public class GraffitiColor {

    public enum Type {
        COLOR, // 颜色值
        BITMAP // 图片
    }

    public  int color;
    public  Bitmap bitmap;
    private Type type;
    private Shader.TileMode tileX = Shader.TileMode.MIRROR;
    private Shader.TileMode tileY = Shader.TileMode.MIRROR;  // 镜像

    public GraffitiColor(int color) {
        type = Type.COLOR;
        this.color = color;
    }

    public GraffitiColor(Bitmap bitmap) {
        type = Type.BITMAP;
        this.bitmap = bitmap;
    }

    public GraffitiColor(Bitmap bitmap, Shader.TileMode tileX, Shader.TileMode tileY) {
        type = Type.BITMAP;
        this.bitmap = bitmap;
        this.tileX = tileX;
        this.tileY = tileY;
    }

    void initColor(Paint paint, Matrix matrix) {
        if (type == Type.COLOR) {
            paint.setColor(color);
        } else if (type == Type.BITMAP) {
            BitmapShader shader = new BitmapShader(bitmap, tileX, tileY);
            shader.setLocalMatrix(matrix);
            paint.setShader(shader);
        }
    }

    public void setColor(int color) {
        type = Type.COLOR;
        this.color = color;
    }

    public void setColor(Bitmap bitmap) {
        type = Type.BITMAP;
        this.bitmap = bitmap;
    }

    public void setColor(Bitmap bitmap, Shader.TileMode tileX, Shader.TileMode tileY) {
        type = Type.BITMAP;
        this.bitmap = bitmap;
        this.tileX = tileX;
        this.tileY = tileY;
    }

    public int getColor() {
        return color;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    public Type getType() {
        return type;
    }

    public GraffitiColor copy() {
        GraffitiColor color = null;
        if (type == Type.COLOR) {
            color = new GraffitiColor(this.color);
        } else {
            color = new GraffitiColor(bitmap);
        }
        color.tileX = tileX;
        color.tileY = tileY;
        return color;
    }
}
